package org.bluedream.core.utils.webUtils;

import org.bluedream.comm.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class ServletUtil {
    private ServletUtil(){
        throw new AssertionError();
    }

    public static boolean isAjaxRequest(HttpServletRequest request){
        String accept = request.getHeader("accept");
        if (accept != null && accept.indexOf("application/json") != -1){
            return true;
        }else {
            String xRequestedWith = request.getHeader("X-Requested-With");
            if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1){
                return true;
            }else {
                String uri = request.getRequestURI();
                if (StringUtils.inStringIgnoreCase(uri , new String[]{".json" , ".xml"})){
                    return true;
                }else {
                    String ajax = request.getParameter("__ajax");
                    return StringUtils.inStringIgnoreCase(ajax, new String[]{"json", "xml"});
                }
            }
        }
    }
}
